package controller

import (
	"encoding/base64"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"log"
	"net/http"
)

// Base64Page base64 首页
func Base64Page(c *gin.Context) {
	c.HTML(http.StatusOK, "base64.html", gin.H{})
}

// ImageToBase64 图片转Base64
func ImageToBase64(c *gin.Context) {
	file, _, err := c.Request.FormFile("file")
	if err != nil {
		log.Printf("GetImage Error:%v \n", err)
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "GetImage Error:" + err.Error(),
		})
		return
	}
	fileBytes, err := ioutil.ReadAll(file)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "Reader To Bytes Error:" + err.Error(),
		})
		return
	}
	toString := base64.StdEncoding.EncodeToString(fileBytes)
	c.JSON(http.StatusOK, gin.H{
		"code": 1,
		"msg":  toString,
	})
}
